package cm.util.action.info;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cm.util.exception.MessageException;
import cm.util.service.GenericService;
import cm.util.service.GenericServiceImpl;



/**
 * 
 * @author 苏玮
 * 
 * 2010-11-29
 * 
 * @param <E>
 * 		实体类别
 * 
 * @param <PK>
 * 		实体的ID类别
 */
public class GenericInfoActionSupport<E> extends InfoActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -8504274191060795667L;

	private static Log log=LogFactory.getLog(GenericInfoActionSupport.class);
	
	public E entity;
	
	protected String id;
	
	protected List<String> ids;
	
	protected GenericService<E> service;
	
	/**
	 * 根据实体类类对象构造Action<br>
	 * 该方法主要在继承该类的Action类中调用<br>
	 * 使用该构造函数将默认使用GenericServiceImpl的service方法<br>
	 * @param entityClass
	 * 			实体类类对象
	 */
	public GenericInfoActionSupport(Class<E> entityClass){
		this(entityClass.getName());
	}
	
	/**
	 * 根据实体类类对象和service对象构造Action<br>
	 * 该方法主要在继承该类的Action类中调用<br>
	 * 使用该构造方法将使用传入的service提供的方法<br>
	 * @param service
	 */
	public GenericInfoActionSupport(Class<E> entityClass,GenericService<E> service){
		this(entityClass.getName(),service);
		
		this.service=service;
	}
	/**
	 * 根据实体类的类名构造Action<br>
	 * 该方法主要在Spring中根据构造函数注入的时候使用<br>
	 * 使用该构造函数将默认使用GenericServiceImpl的service方法<br>
	 * @param entityClassName
	 */
	@SuppressWarnings("unchecked")
	public GenericInfoActionSupport(String entityClassName){
		try{
			/**
			 * 初始化实体对象
			 */
			entity=(E)Class.forName(entityClassName).newInstance();
			
		} catch (ClassNotFoundException e){
			log.error("不存在名为:"+entityClassName+"的类！", e);
		} catch (InstantiationException e) {
			log.error("实例化"+entityClassName+"对象出错！", e);
		} catch (IllegalAccessException e) {
			log.error(e);
		}
		service=new GenericServiceImpl<E>(entityClassName);
	}

	/**
	 * 根据实体类的类名构造Action<br>
	 * 该方法主要在Spring中根据构造函数注入的时候使用<br>
	 * 使用该构造方法将使用传入的service提供的方法<br>
	 * @param entityClassName
	 */
	@SuppressWarnings("unchecked")
	public GenericInfoActionSupport(String entityClassName,GenericService<E> service){
		try{
			/**
			 * 初始化实体对象
			 */
			entity=(E)Class.forName(entityClassName).newInstance();
			
		} catch (ClassNotFoundException e){
			log.error("不存在名为:"+entityClassName+"的类！", e);
		} catch (InstantiationException e) {
			log.error("实例化"+entityClassName+"对象出错！", e);
		} catch (IllegalAccessException e) {
			log.error(e);
		}
		this.service=service;
	}
	
	@Override
	public String add() throws MessageException {
		service.add(getEntity());
		return ADD_SUCCESS;
	}

	@Override
	public String delete() throws MessageException {
		if(getIds().size()==0)
			throw new MessageException("ID号不能为空！");
		service.delete(getIds());
		return DELETE_SUCCESS;
	}

	@Override
	public String detail() throws MessageException {
		if(isNull(getId()))
			throw new MessageException("ID号不能为空！");
		this.setEntity(service.detail(getId()));
		return DETAIL_SUCCESS;
	}

	@Override
	public String to_update() throws MessageException {
		if(isNull(getId()))
			throw new MessageException("ID号不能为空！");
		this.setEntity(service.detail(getId()));
		return TO_UPDATE_SUCCESS;
	}

	@Override
	public String update() throws MessageException {
		service.update(getEntity());
		return UPDATE_SUCCESS;
	}
	
	public E getEntity(){
		return entity;
	}

	public void setEntity(E entity){
		this.entity=entity;
	}
	




	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public List<String> getIds() {
		return ids;
	}


	public void setIds(List<String> ids) {
		this.ids = ids;
	}
	public void setService(GenericService<E> service) {
		this.service = service;
	}
	public GenericService<E> getService() {
		return service;
	}
	
}
